<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    发送名称
                </label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input layui-form-danger" name="sendname" type="text"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    物品列表
                </label>
                <div class="layui-input-block">
                    <textarea name="sendgoodsls" id="sendgoodsls"  lay-verify="sendgoodsls" placeholder="不需要填写！！！点击按钮进行添加！" class="layui-textarea" readonly></textarea>
                </div>
            </div>
            <div class="layui-form-item" >
                <div class="layui-input-block" id='listbox' style="border: 1px dashed #fffb00;">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <div class="layui-input-inline">
                    <select name="goodsls" id="goodsls" lay-search>
                        <option value="">物品列表(可输入后选择)</option>
                        {volist name="ItemLs" id="ItemLs"}
                        <option value="{$ItemLs.itemid}">{$ItemLs.itemname}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input layui-form-danger" name="goodsnum" id="goodsnum" type="text" />
                </div>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn layui-btn-normal" id="btnAddgoods" >添加</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    备注
                </label>
                <div class="layui-input-block">
                    <textarea name="remake" id="remake"  lay-verify="remake" placeholder="备注内容" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'jquery', 'layedit', 'uploads'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let layedit = layui.layedit
        layedit.set({
            uploadImage: {
                url: "{$Request.root}/index/upload"
            }
        });
        //建立编辑器
        
        form.on('submit(save)', function (data) {
            console.log(data.field);
            $.ajax({
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (res) {
                    //判断有没有权限
                    if (res && res.code == 999) {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 2000,
                        })
                        return false;
                    } else if (res.code == 200) {
                        layer.msg(res.msg,{icon:1,time:1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("dataTable");
                        });
                    } else {
                        layer.msg(res.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });


        var itemList = {}

        $('#btnAddgoods').click(function () {
            var itemid = $('#goodsls').val().trim();
            var itemnum = $('#goodsnum').val().trim();

            if (itemid == '') {
                layer.msg('请选择物品。。');
                return false;
            }
            if (itemnum == '') {
                layer.msg('请填入数量。');
                return false;
            }

            var itemName = $('#goodsls').find("option:selected").text().trim();
            itemName=itemName.replace("[","");
            itemName=itemName.replace("]","");
            itemName=itemName.replace("'","");

            // 创建一个新按钮元素
            var newButton = document.createElement('button');
            newButton.setAttribute('type', 'button');
            newButton.setAttribute('class', 'layui-btn layui-btn-primary layui-border-black');
            newButton.innerText = itemName + '=>' + itemnum;
            // 向itemList对象插入数据
            itemList[itemid] = {name: itemName, num: parseInt(itemnum)};
            
            if (itemList == {} || itemList === undefined) {
                $('#sendgoodsls').val('')
            }else{
                $('#sendgoodsls').val(JSON.stringify(itemList)) 
            }
            
            // 添加点击事件，用于删除按钮自身
            newButton.addEventListener('click', function () {
                // 从itemList中删除对应数据
                delete itemList[itemid];
                
                var buttons = document.getElementsByClassName('layui-btn');
                for (var i = 0; i < buttons.length; i++) {
                    var buttonText = buttons[i].innerText;
                    if (buttonText.includes(itemName) && buttonText.includes(itemnum)) {
                        buttons[i].parentNode.removeChild(buttons[i]);
                        i--; // 因为删除了一个按钮，需要将索引减一
                    }
                }

                if (itemList == {} || itemList === undefined) {
                    $('#sendgoodsls').val('')
                }else{
                    $('#sendgoodsls').val(JSON.stringify(itemList)) 
                }
            });

            // 将新按钮添加到listbox中
            var myDiv = document.getElementById("listbox");
            myDiv.appendChild(newButton);
        });

    })
</script>
</body>
</html>